﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;

public partial class ContentType : System.Web.UI.Page
{
    //protected string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E://spbtzb.mdb';";
    protected string strConnection = spbtzb.ConString.ConnString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["type"] != null)
            {
                string type = Request.Params["type"].ToString();
                BindParts(type);
                BindTopList(type);
                BindControl(type);
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }
    }
    protected void BindTopList(string type)
    {
        //可以肯定是Type是ParentType
    //  String strCmd = "SELECT TOP 10 A.ID, A.Title, A.CreateTime,A.ViewTimes FROM Contents A, ContentType B WHERE A.TypeCode = B.TypeCode AND B.ParentType = '" + type + "' Order By A.ViewTimes DESC, A.ID DESC";
        String strCmd = "SELECT TOP 10 C.ID, C.Title, C.CreateTime,C.ViewTimes, C.TypeCode FROM Contents AS C, ContentAndType AS CT, ContentType AS T WHERE CT.TypeID=T.ID And C.ID = CT.ContentID AND  T.ParentType='" + type + "' ORDER BY C.ViewTimes DESC , C.ID DESC";
        DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text, strCmd);
        if (dt == null) return;
        rpTopList.DataSource = dt;
        rpTopList.DataBind();
    }
    protected void rpParts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rpList = (Repeater)e.Item.FindControl("rpList");
            DataRowView dv = (DataRowView)e.Item.DataItem;

            if (rpList != null)
            {
                DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
                    //      "SELECT TOP 5 ID, Title, CreateTime FROM Contents WHERE TypeCode = '" + dv["TypeCode"].ToString() + "' Order By CreateTime DESC, [ID] DESC "
                      "SELECT TOP 5 C.ID, C.Title, C.CreateTime,C.ViewTimes,T.TypeCode FROM Contents AS C, ContentAndType AS CT, ContentType AS T WHERE CT.TypeID=T.ID And C.ID = CT.ContentID AND T.TypeCode='" + dv["TypeCode"].ToString() + "'ORDER BY C.ViewTimes DESC , C.ID DESC");
                if (dt == null) return;
                if (dt.Rows.Count == 0)
                {
                    HtmlGenericControl div = (HtmlGenericControl)e.Item.FindControl("dvEmpty");
                    div.InnerHtml += "&nbsp;&nbsp;此栏目暂无信息";
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
        }
    }
    protected void BindParts(string parentType)
    {
        DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
                            "SELECT ID, TypeCode, TypeName FROM ContentType WHERE IsSingle <> 1 and ParentType = '" + parentType + "'");
        if (dt == null)
        {
            Response.Redirect("ContentList.aspx?type=" + parentType);
        }
        if (dt.Rows.Count == 0)
        {
            Response.Redirect("ContentList.aspx?type=" + parentType);
        }
        rpParts.DataSource = dt;
        rpParts.DataBind();

    }
    
    protected void BindControl(string type)
    {
        string typeName = Common.GetTypeNameByCode(type);
        ltTitle.Text = typeName;
        this.Page.Title = typeName;
        dvNav.InnerHtml += "<a href ='ContentType.aspx?type=" + type + "'>" + typeName + "</a>";
    }
    protected string CutString(string title)
    {
        return Common.CutString(title);
    }
    protected string ConvertTime(object obj)
    {
        if (obj is Nullable)
            return "";
        try
        {
            DateTime dt = DateTime.Parse(obj.ToString());
            return "(" + dt.Year.ToString() + "-" + dt.Month.ToString() + "-" + dt.Day.ToString() + ")";
        }
        catch { }
        return "";

    }

    public DataTable ChildParts
    {
        get
        {
            string type = Request.Params["type"].ToString();
            DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
                            "SELECT ID, TypeCode, TypeName FROM ContentType WHERE ParentType = '" + type + "'");
            return dt;
        }
    }

}
